package com.linh.trade.gui;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.image.TileObserver;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import com.linh.trade.Constants;
import com.linh.trade.data.NewPointListener;
import com.linh.trade.data.Point;
import com.linh.trade.valuation.Valuator;

public class ReportPanel extends JPanel implements NewPointListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Valuator valuator;

	private JLabel pnlLabel;

	private JLabel realizedPnlLabel;

	public ReportPanel(Valuator valuator) {
		super();
		this.valuator = valuator;
		TitledBorder border = BorderFactory.createTitledBorder("Report");
		setBorder(border);
		setLayout(new GridLayout(2, 2));
		add(new JLabel("PnL"));
		pnlLabel = new JLabel();
		add(pnlLabel);
		add(new JLabel("Realized PnL"));
		realizedPnlLabel = new JLabel();
		add(realizedPnlLabel);
	}

	public void newPoint(Point p) {

		displayPnl(p);
	}

	void displayPnl(Point p) {

		double realizedPnl = valuator.realizedPnL();
		double pnl = realizedPnl;
		if (p != null) {

			pnl = valuator.pnlAt(p);
		}

		if (pnl >= 0) {
			pnlLabel.setForeground(Color.green);
		} else {
			pnlLabel.setForeground(Color.red);
		}

		if (realizedPnl >= 0) {
			realizedPnlLabel.setForeground(Color.green);
		} else {
			realizedPnlLabel.setForeground(Color.red);
		}
		pnlLabel.setText(Constants.AMOUNT_FORMAT.format(pnl));
		realizedPnlLabel.setText(Constants.AMOUNT_FORMAT.format(realizedPnl));
		repaint();
	}

	public void endOfData() {
		displayPnl(null);
	}

}
